package com.example.helper;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.accessibility.AccessibilityNodeInfo;

import java.io.DataOutputStream;
import java.io.OutputStream;
import java.util.List;

public class utils {
    public void execShellCmd(String cmd) {
        try {
            // 申请获取root权限，这一步很重要，不然会没有作用
            Process process = Runtime.getRuntime().exec("su");
            // 获取输出流
            OutputStream outputStream = process.getOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(
                    outputStream);
            dataOutputStream.writeBytes(cmd);
            dataOutputStream.flush();
            dataOutputStream.close();
            outputStream.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    public void Click(List<AccessibilityNodeInfo> Node){
        if(Node.size()!=0) {
            Rect rect = new Rect();
            Node.get(0).getBoundsInScreen(rect);
            String x = String.valueOf((rect.left + rect.right) / 2);
            String y = String.valueOf((rect.top + rect.bottom) / 2);
            execShellCmd("input tap " + x + " " + y);
        }
    }
    public void Back(List<AccessibilityNodeInfo> Node){
        if(Node.size()!=0) {
            execShellCmd("input keyevent 4");
        }
    }
    public void set_text(AccessibilityNodeInfo Node,String word){
        Bundle arguments =new Bundle();
        arguments.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,word);
        Node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,arguments);
    }
    public void swipe(int x1,int y1,int x2,int y2,int time){
        execShellCmd("input swipe "+x1+" "+y1+" "+x2+" "+y2+" "+time);
    }
}
